package com.cneport.das.common.utils;

import cn.hutool.core.util.ReflectUtil;

import java.lang.reflect.Field;

/**
 * @Desc Bean 操作工具
 * @Version 1.0
 * @Company xxxxx  公司
 * @Author cuipeng
 * @Date 2021/10/26 16:07
 */
public class BeanUtil {

    public static <T> T copy(Object object, Class<T> clazz) {
        return cn.hutool.core.bean.BeanUtil.toBean(object, clazz);
    }

    /**
     * 拷贝对象，将第一个对象的非空值（跳过null,或字符串”“）拷贝到第二对象对应的属性上，
     *
     * @param object
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T copySkipNull(Object object, Object obj, Class<T> clazz) {
        T t = (T) object;
        Field[] fields = ReflectUtil.getFields(clazz);
        try {
            for (Field field : fields) {
                Object fieldValue = ReflectUtil.getFieldValue(object, field);
                if (fieldValue == null || "".equals(fieldValue.toString())) {
                    continue;
                } else {
                    field.set(obj, fieldValue);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return (T) obj;
    }
}
